var shotsForLocalStorage = [];

var Replay = {
	getLocalGameNumber : function() {
		if((typeof(localStorage.BSHIPS_gamesNumber)=="undefined") || (localStorage.BSHIPS_gamesNumber == null)) {
			return 1;
		}
		else {
			return parseInt(localStorage.BSHIPS_gamesNumber) + 1;
		}
	},
	increaseNumberOfGames : function() {
		if((typeof(localStorage.BSHIPS_gamesNumber)=="undefined") || (localStorage.BSHIPS_gamesNumber == null)) {
			localStorage.BSHIPS_gamesNumber = "1";
		}
		else {
			var number = parseInt(localStorage.BSHIPS_gamesNumber) + 1;
			localStorage.BSHIPS_gamesNumber = String(number);
		}
	},

	fleetsToLocalStorage : function() {
		for (placed in placedFleets) {
			localStorage.setItem("BSHIPS_" + String(localGameId) + "_fleet" + String(placedFleets[placed].id), String(placedFleets[placed].top) + "_" + String(placedFleets[placed].left) + "_" + String(placedFleets[placed].size) + "_" + String(placedFleets[placed].orientation));
		}
	},
	
	uploadDataToLocalStorage : function() {
		Replay.increaseNumberOfGames();
		Replay.fleetsToLocalStorage();
		for (kirje in shotsForLocalStorage) {
			localStorage.setItem(kirje, shotsForLocalStorage[kirje]);
		}
	},

	playerReplayFire : function(stringArray){
		var x = stringArray[0];
		var y = stringArray[1];
		
		var result = stringArray[3];
		
		if (result == "0") {
			canvas.drawShot("missed", null, x, y, "enemy");
		}
		else if (result == "1") {
			canvas.drawShot("hit", "enemyHit", x, y, "enemy");
		}
		else if (result == "2") {
			var tempfleet = new Fleet(parseInt(stringArray[4]), parseInt(stringArray[5]), "temp");
			tempfleet.orientation = parseInt(stringArray[8]);
			tempfleet.setTopLeft(parseInt(stringArray[6]), parseInt(stringArray[7]));
			canvas.displayScore("player", player.sinked);
			canvas.removeHitShots("enemyHit", tempfleet);
			canvas.drawFleet("sinkedEnemyFleet", "sinked", tempfleet, "enemy");
			player.sinked++;
			canvas.displayScore("player", player.sinked);
		}
		player.total++;
	},

	enemyReplayFire : function(stringArray){
		var x = parseInt(stringArray[0]);
		var y = parseInt(stringArray[1]);
		var shotStatus = parseInt(stringArray[3]);
		if (shotStatus == 0) {
			canvas.drawShot("missed", null, x, y, "player");
		}
		
		if (shotStatus == 1) {
			canvas.drawShot("hit", "hit", x, y, "player");
			enemy.hit++;
		}
		
		if (shotStatus == 2) {
			var id = parseInt(stringArray[4]);
			enemy.sinked++;
			canvas.displayScore("enemy", enemy.sinked);
			canvas.removeHitShots("hit", placedFleets["fleet"+ stringArray[4]]);
			canvas.drawFleet("sinkedEnemyFleet", "sinked", placedFleets["fleet"+ stringArray[4]], "player");
			enemy.hit++;
		}
		
		enemy.total++;
	},

	initReplay : function() {
		move.stopListening();
		player = new Player(1);
		enemy = new Player(2);
		localGameId = Replay.getQueryVariable("localGameId");
		var fleetObjects = [];
		for (var i = 0; i < 10; i++) {
			var fleetString = localStorage.getItem("BSHIPS_" + String(localGameId) + "_fleet" + String(i+1));
			var splitString = fleetString.split("_");
			var fleet = new Fleet(i+1, parseInt(splitString[2]), "name");
			fleet.setTopLeft(parseInt(splitString[0]), parseInt(splitString[1]));
			fleet.setOrientation(parseInt(splitString[3]));
			fleetObjects["fleet"+fleet.id] = fleet;
		}
		placedFleets = fleetObjects;
		canvas.drawFleets();
	},

	replayLoop : function(i) {
		var shotString = localStorage.getItem("BSHIPS_" + String(localGameId) + "_" + String(i));
		if (shotString == null) {
			return;
		}
		var splitShot = shotString.split("_");
		if (splitShot[2] == "player") {
			Replay.playerReplayFire(splitShot);
		}
		else {
			Replay.enemyReplayFire(splitShot);
		}
		i++;
		setTimeout("Replay.replayLoop( "+ String(i) + ")",1000);
	},

	startReplay : function() {
		canvas.drawEnemyTable();
		if (typeof(localStorage.getItem("BSHIPS_" + String(localGameId) + "_" + String(1))) != "undefined") {
			setTimeout("Replay.replayLoop(0)", 500);
		}
	},

	getQueryVariable : function(variable) {
		var query = window.location.search.substring(1);
		var vars = query.split("&");
		for (var i = 0; i < vars.length; i++) {
			var pair = vars[i].split("=");
			if (pair[0] == variable) {
				return unescape(pair[1]);
			}
		}
		alert('Query Variable ' + variable + ' not found');
	}
};